package com.epfresh.supplier.dialog;

import android.content.Context;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.RelativeLayout;

import com.epfresh.api.widget.Dialog.BaseDialogView;
import com.epfresh.supplier.R;
import com.epfresh.supplier.adapter.AgentFilterContentViewBinder;
import com.epfresh.supplier.adapter.AgentFilterTitleViewBinder;
import com.epfresh.supplier.entity.BuyerFilter;

import java.util.ArrayList;
import java.util.List;

import multitype.MultiTypeAdapter;
import multitype.OnRecyclerViewItemClickListener;

/**
 * Created by wzh on 16-7-13.
 */
public class FilterCategoryDialog extends BaseDialogView {
    private int height;

    public FilterCategoryDialog(Context context) {
        super(context);
        setInAnim(true);
        setOutAnim(true);
        inAnim.setDuration(0);
        outAnim.setDuration(0);
    }

    public FilterCityDialog.SelectCondition selectCondition;
    public interface SelectCondition {
        void selectCity(String condition);
        void selectCategory(String condition);
    }

    public FilterCityDialog.SelectCondition getSelectCondition() {
        return selectCondition;
    }

    public void setSelectCondition(FilterCityDialog.SelectCondition selectCondition) {
        this.selectCondition = selectCondition;
    }

/*    public Animation getInAnimation() {

        return AnimationUtils.loadAnimation(this.context, com.epfresh.api.R.anim.anim_top_push_in);
    }

    public Animation getOutAnimation() {
        return AnimationUtils.loadAnimation(this.context, com.epfresh.api.R.anim.anim_top_push_out);
    }*/

    @Override
    protected void startInAnimation() {
        if (recyclerView != null) {
            recyclerView.startAnimation(inAnim);
        }
    }

    @Override
    protected void startOutAnimation() {
        if (recyclerView != null) {
            recyclerView.startAnimation(outAnim);
        }
    }

    @Override
    protected void initRes() {
        outContainerRes = R.id.f_out_container;
        containerRes = R.id.f_content_container;
        rootRes = R.layout.dialog_content_view;
        rootBgRes = R.id.f_v_content_bg;
    }

    RelativeLayout.LayoutParams lp;

    @Override
    protected void createdView() {
        View v = LayoutInflater.from(context).inflate(R.layout.dialog_recycler_layout, contentContainer);
        FrameLayout.LayoutParams lp = (FrameLayout.LayoutParams) rootView.getLayoutParams();
        lp.topMargin = height;
        setCancelable(true);
        initView();
    }

    RecyclerView recyclerView;
    MultiTypeAdapter mMultiAdapter;
    AgentFilterContentViewBinder itemViewBinder;
    AgentFilterTitleViewBinder titleViewBinder;
    GridLayoutManager mManager;
    List<Object> mItems = new ArrayList<>();

    private void initView() {
        recyclerView = findView(R.id.rv_dialog_filter);
        mManager = new GridLayoutManager(context, 4);
        GridLayoutManager.SpanSizeLookup spanSizeLookup = new GridLayoutManager.SpanSizeLookup() {
            @Override
            public int getSpanSize(int position) {
                Object item = mItems.get(position);
                return (item instanceof BuyerFilter.NamesBean.ChildBean) ? 1 : 4;
            }
        };
        mManager.setSpanSizeLookup(spanSizeLookup);
        recyclerView.setLayoutManager(mManager);
        mMultiAdapter = new MultiTypeAdapter();
        itemViewBinder = new AgentFilterContentViewBinder();
        titleViewBinder = new AgentFilterTitleViewBinder();
        mMultiAdapter.register(BuyerFilter.NamesBean.class,titleViewBinder);
        mMultiAdapter.register(BuyerFilter.NamesBean.ChildBean.class, itemViewBinder);
        mMultiAdapter.setItems(mItems);
        recyclerView.setAdapter(mMultiAdapter);
        mMultiAdapter.setItems(mItems);

        itemViewBinder.setOnRecyclerViewItemClickListener(new OnRecyclerViewItemClickListener() {
            @Override
            public void onItemClick(View view, int position, Object data) {
                switch (view.getId()){
                    case R.id.tv_dialog_filter_content:
                        if(data instanceof BuyerFilter.NamesBean.ChildBean){
                            if(((BuyerFilter.NamesBean.ChildBean) data).isSelectFlag()){
                                ((BuyerFilter.NamesBean.ChildBean) data).setSelectFlag(false);
                                selectCondition.selectCategory("","品类");
                            }else{
                                for(int i=0;i<mItems.size();i++){
                                    if(mItems.get(i) instanceof BuyerFilter.NamesBean.ChildBean)
                                        ((BuyerFilter.NamesBean.ChildBean)mItems.get(i)).setSelectFlag(false);
                                }
                                ((BuyerFilter.NamesBean.ChildBean) data).setSelectFlag(true);
                                selectCondition.selectCategory(((BuyerFilter.NamesBean.ChildBean) data).getId(),((BuyerFilter.NamesBean.ChildBean) data).getName());
                            }
                            mMultiAdapter.notifyDataSetChanged();
                        }
                        break;
            }
        }});



        mMultiAdapter.notifyDataSetChanged();
    }
    public void show(List<BuyerFilter.NamesBean> list, int height) {
        this.height = height;
        mItems.clear();
        if (lp != null)
            lp.topMargin = height;
        for(int i=0;i<list.size();i++){
            if (i == 0) {
                mItems.addAll(list.get(i).getChild());
            }else {
                mItems.add(list.get(i));
                mItems.addAll(list.get(i).getChild());
            }
        }
        show();
    }
    //测试要求，进入其他界面返回来以后要设置选中的三级分类的状态
    public void setCategorySelect(String name){
        for(int i=0;i<mItems.size();i++){
            Object data = mItems.get(i);
           if (data instanceof BuyerFilter.NamesBean.ChildBean){
               if (((BuyerFilter.NamesBean.ChildBean) data).getName().equals(name)){
                   ((BuyerFilter.NamesBean.ChildBean) data).setSelectFlag(true);
               }
           }
        }
        mMultiAdapter.notifyDataSetChanged();
    }
    public void show(List<BuyerFilter.NamesBean> list, View faterView) {
        this.height = height;
        mItems.clear();
        if (lp != null)
            lp.topMargin = height;
        for(int i=0;i<list.size();i++){
            mItems.add(list.get(i));
            mItems.addAll(list.get(i).getChild());
        }
        show();
    }


    @Override
    public void initBefore() {
        setGravity(Gravity.TOP);
    }

    @Override
    protected void initAnim() {
        super.initAnim();
    }


    public void setHeight(int height) {
        this.height = height;
        lp.topMargin = height;
    }
}
